package 代码随想2._2链表;

/**
 * @author XXX
 * @date 2024-01-14 14:02
 */

/**
 * https://leetcode.cn/problems/linked-list-cycle-ii/description/
 */
public class _7环形链表II {
    public ListNode detectCycle(ListNode head) {
        if (head == null)
            return head;
        ListNode fast = head;
        ListNode slow = head;
        while (fast!=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow)
                break;
        }
        if(fast==null || fast.next==null)
            return null;
        fast = head;
        while (fast!=slow){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
